package com.weds.edwx .web;

import com.alibaba.fastjson.JSONObject;
import com.weds.core.utils.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import javax.validation.Valid;

import com.weds.core.base.BaseController;
import com.weds.core.annotation.Logs;
import com.weds.core.resp.JsonResult;
import com.weds.core.base.BaseCommPager;
import com.weds.core.base.BasePageSearch;


import com.weds.edwx.entity .DineUserRoleEntity;
import com.weds.edwx.service .DineUserRoleService;

/**
 * @Author sxm
 * @Description DineUserRole管理
 * @Date 2020-04-06
 */
@RestController
@RequestMapping(value="/dineUserRole")
@Api(value = "DineUserRole管理", description = "DineUserRole管理")
public class DineUserRoleController extends BaseController{
	@Autowired
	private DineUserRoleService dineUserRoleService;

    
	private Logger log = LogManager.getLogger();

    @Logs
	@ApiOperation(value = "新增DineUserRole信息", notes = "新增DineUserRole信息")
	@RequestMapping(value = "/insert", method = RequestMethod.PUT)
	public JsonResult<Object> insert(@RequestBody @Valid DineUserRoleEntity record) {
        				dineUserRoleService.insertSelective(record);
		return succMsg();
	}

	@Logs
	@ApiOperation(value = "更新DineUserRole信息", notes = "更新DineUserRole信息")
	@RequestMapping(value = "/updateByPrimaryKey", method = RequestMethod.POST)
	public JsonResult<Object> updateByPrimaryKey(@RequestBody @Valid DineUserRoleEntity record) {
        		dineUserRoleService.updateByPrimaryKeySelective(record);
		return succMsg();
	}

		@Logs
	@ApiOperation(value = "查询DineUserRole清单", notes = "查询DineUserRole清单")
	@RequestMapping(value = "/selectListPageByEntity", method = RequestMethod.POST)
	public JsonResult<BaseCommPager<DineUserRoleEntity>> selectListPageByEntity(@RequestBody BasePageSearch<DineUserRoleEntity> record) {
		setPageHelper(record);
		DineUserRoleEntity entity = record.getSearch();
        if (entity == null) {
        	entity = new DineUserRoleEntity();
        }
                List<DineUserRoleEntity> list = dineUserRoleService.selectListByEntity(entity);
        		return succMsgData(new BaseCommPager<DineUserRoleEntity>(list));
	}

    @Logs
    @ApiOperation(value = "查询DineUserRole列表", notes = "查询DineUserRole列表")
    @RequestMapping(value = "/selectListByEntity", method = RequestMethod.POST)
    public JsonResult<List<DineUserRoleEntity>> selectListByEntity(@RequestBody DineUserRoleEntity record) {
                List<DineUserRoleEntity> list = dineUserRoleService.selectListByEntity(record);
                return succMsgData(list);
    }
	
		@Logs
	@ApiOperation(value = "删除DineUserRole信息", notes = "删除DineUserRole信息")
	@RequestMapping(value = "/deleteByPrimaryKey", method = RequestMethod.DELETE)
	public JsonResult<Object> deleteByPrimaryKey(@RequestParam Long userSerial, @RequestParam String role) {
        dineUserRoleService.deleteByPrimaryKey(userSerial, role);
        return succMsg();
	}

	@Logs
	@ApiOperation(value = "查询订餐权限", notes = "查询DineUserRole信息")
	@RequestMapping(value = "/selectOrderAuthority", method = RequestMethod.GET)
	public JsonResult<DineUserRoleEntity> selectByPrimaryKey(@RequestParam Long userSerial) {
		Integer usercount = dineUserRoleService.selectAllowDineUser();
		DineUserRoleEntity roleEntity = new DineUserRoleEntity();
		if(usercount!=null && usercount > 0){ //有设置人员
			DineUserRoleEntity entity = dineUserRoleService.selectByPrimaryKey(userSerial, "1012");
			if(entity == null){ //没有该人员权限
				roleEntity.setOrderFlag("0");
				return succMsgData(roleEntity);
			}
		}
		//其他情况均为 有权限
		roleEntity.setOrderFlag("1");
		return succMsgData(roleEntity);
	}
	

		@Logs
	@ApiOperation(value = "批量删除DineUserRole信息", notes = "批量删除DineUserRole信息")
	@RequestMapping(value = "/deleteBatchByKeys", method = RequestMethod.POST)
	public JsonResult<Object> deleteBatchByKeys(@RequestBody List<DineUserRoleEntity> list) {
        List<DineUserRoleEntity> delList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            delList.add(list.get(i));
            if (delList.size() == 20 || i == list.size() - 1) {
                dineUserRoleService.deleteBatchByKeys(delList);
                delList.clear();
            }
        }
		return succMsg();
	}
	
		@Logs
	@ApiOperation(value = "批量新增DineUserRole信息", notes = "批量新增DineUserRole信息")
	@RequestMapping(value = "/insertBatch", method = RequestMethod.PUT)
	public JsonResult<Object> insertBatch(@RequestParam String data) {
		List<DineUserRoleEntity> list	= JSONObject.parseArray(data, DineUserRoleEntity.class);
		dineUserRoleService.insertBatch(list);
		return succMsg();
	}
	}